package com.yaozheng.vocationaltraining.fragment;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.iview.integral.IMyIntegralView;
import com.yaozheng.vocationaltraining.service.MyIntegralService;
import com.yaozheng.vocationaltraining.service.impl.MyIntegralServiceImpl;
import com.yaozheng.vocationaltraining.utils.ACache;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 我的积分
 *
 * @author Administrator
 */
@EFragment(R.layout.fragment_my_integral)
public class MyIntegralFragment extends BaseFragment implements IMyIntegralView {
    @ViewById
    ImageView setUpImage, changeAvatarImageView, logoutImageView, userAvatarImage;
    @ViewById
    TextView integralDetailsText, headTitleText, maxIntegralText,
            quarterIntegralText;
    @Bean(MyIntegralServiceImpl.class)
    MyIntegralService myIntegralService;
    ACache aCache;
    BitmapLoader userBitmapLoader;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    /**
     * 初始化View
     */
    @AfterViews
    public void initView() {
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);
        aCache = ACache.get(getContext());
        String userAvatar = aCache.getAsString("userAvatar");
        userBitmapLoader.displayImage(userAvatar, userAvatarImage);

        setUpImage.setVisibility(View.GONE);
        changeAvatarImageView.setVisibility(View.GONE);
        logoutImageView.setVisibility(View.GONE);

        headTitleText.setText("我的积分");
        // maxIntegralText.setText(Html.fromHtml(getString(
        // R.string.maxIntegralStr,
        // getString(R.string.defaultSpecialFontColorStr, "252080"))));
        //
        // quarterIntegralText.setText(Html.fromHtml(getString(
        // R.string.quarterIntegralStr,
        // getString(R.string.defaultSpecialFontColorStr, "35698"))));
        myIntegralService.init(this);
        setIntegralValue(null);
        loadData();
    }

    public void loadData() {
        myIntegralService.getUserScore();
    }

    /**
     * 打开积分明细
     */
    @Click(R.id.integralDetailsText)
    public void openIntegralDetailsFragment() {
        getHomeActivity().openIntegralDetailsFragment("积分明细",0);
    }

    /**
     * 打开积分名称
     */
    @Click(R.id.integralTypeText)
    public void openIntegralTypeListFragment() {
        getHomeActivity().openIntegralTypeListFragment();
    }

    /**
     * 打开积分规则
     */
    @Click(R.id.integralRuleText)
    public void openIntegralRuleFragment() {
        getHomeActivity().openIntegralRuleFragment();
    }

    /**
     * 积分抽奖
     */
    @Click(R.id.integralLotteryText)
    public void openIntegralLotteryFragment() {
        getHomeActivity().openIntegralLotteryFragment();
    }

    @Click(R.id.signTextView)
    public void signTextViewClick() {
        Intent intent = new Intent();
        intent.setClassName(getContext(), Constants.HOME_SIGN_TASK_ACTIVITY_CLASS_NAME);
        startActivity(intent);
    }

    public void setIntegralValue(JSONObject jsonObject) {
        maxIntegralText.setText(String.valueOf(
                TypeUtils.getJsonInteger(jsonObject, "total", 0)));
        quarterIntegralText.setText(
                String.valueOf(TypeUtils.getJsonInteger(jsonObject, "quarter", 0)));
    }

    @Override
    public boolean isResponseResult() {
        return isAdded();
    }

    @UiThread
    @Override
    public void userScoreSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        setIntegralValue(TypeUtils.getJsonObject(jsonObject, "data", "score"));
    }

    @UiThread
    @Override
    public void userScoreError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }
}
